package us.mooresr.webtroopmanager.client.ui;




import us.mooresr.webtroopmanager.client.place.PeopleListPlace;
import us.mooresr.webtroopmanager.shared.Person;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SimpleCheckBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;


public class EditPersonViewImpl extends Composite implements EditPersonView {
	private Presenter presenter;
	private Long personId;
	
	private static EditPersonUiBinder uiBinder = GWT.create(EditPersonUiBinder.class);
	
	interface EditPersonUiBinder extends UiBinder<Widget, EditPersonViewImpl> {
		
	}
	
	@UiField
	TextBox firstNameTextBox;
	
	@UiField 
	TextBox lastNameTextBox;
	
	@UiField 
	ListBox typeComboBox;

	@UiField 
	ListBox genderComboBox;

	@UiField 
	TextBox driversLicenseTextBox;
	
	@UiField
	TextBox bsaIdTextBox;

	@UiField 
	DateBox healthHistoryDateBox;

	@UiField 
	DateBox holdHarmlessDateBox;

	@UiField 
	DateBox medicalExamDateBox;

	@UiField 
	TextBox cellPhoneTextBox;

	@UiField 
	TextBox workPhoneTextBox;

	@UiField 
	TextBox homePhoneTextBox;

	@UiField 
	TextBox emailAddressTextBox;

	@UiField 
	DateBox dateOfBirthDateBox;

	@UiField 
	SimpleCheckBox activeCheckBox;
	
	@UiHandler("okButton")
	void onOkClick(ClickEvent e) {
		// TODO:  Validate the data
		Person p = new Person(personId,
				firstNameTextBox.getText(),
				lastNameTextBox.getText(),
				typeComboBox.getSelectedIndex(),
				healthHistoryDateBox.getValue(),
				medicalExamDateBox.getValue(),
				holdHarmlessDateBox.getValue(),
				driversLicenseTextBox.getText(),
				activeCheckBox.getValue(),
				(genderComboBox.getSelectedIndex() == 0 ? "M" : "F"),
				dateOfBirthDateBox.getValue(), 
				homePhoneTextBox.getText(),
				workPhoneTextBox.getText(), 
				cellPhoneTextBox.getText(),
				null, // Address
				null, // City
				null, // State 
				null, // Zip
				emailAddressTextBox.getText(),
				bsaIdTextBox.getText());

		presenter.addPerson(p);

		presenter.goTo(new PeopleListPlace());		
	}
	
	@UiHandler("cancelButton")
	void onCancelClick(ClickEvent e) {
		presenter.goTo(new PeopleListPlace());	
	}
	
	public EditPersonViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		
		typeComboBox.addItem("Scout");
		typeComboBox.addItem("Adult");
		
		genderComboBox.addItem("Male");
		genderComboBox.addItem("Female");
		
		DateTimeFormat myDateTimeFormat = DateTimeFormat.getFormat("M/d/yyyy");
		DateBox.DefaultFormat myFormat = new DateBox.DefaultFormat(myDateTimeFormat);
		healthHistoryDateBox.setFormat(myFormat);
		medicalExamDateBox.setFormat(myFormat);
		holdHarmlessDateBox.setFormat(myFormat);
		dateOfBirthDateBox.setFormat(myFormat);
		
		
	}


	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;

	}
	
	
	public void setData(Person person) {
		if (person == null) {
			// This is an Add
			personId = null;
			firstNameTextBox.setValue("");
			lastNameTextBox.setValue("");
			typeComboBox.setSelectedIndex(0);
			genderComboBox.setSelectedIndex(0);
			driversLicenseTextBox.setValue("");
			healthHistoryDateBox.setValue(null);
			medicalExamDateBox.setValue(null);
			holdHarmlessDateBox.setValue(null);
			cellPhoneTextBox.setValue("");
			workPhoneTextBox.setValue("");
			homePhoneTextBox.setValue("");
			emailAddressTextBox.setValue("");
			dateOfBirthDateBox.setValue(null);
			activeCheckBox.setValue(true);			
			bsaIdTextBox.setValue("");
		} else {
			// This is an Edit
			personId = person.getId();
			firstNameTextBox.setValue(person.getFirstName());
			lastNameTextBox.setValue(person.getLastName());
			typeComboBox.setSelectedIndex(person.getType());
			if (person.getGender().equals("M")) {
				genderComboBox.setSelectedIndex(0);
			} else {
				genderComboBox.setSelectedIndex(1);
			}
			driversLicenseTextBox.setValue(person.getDriversLicense());
			healthHistoryDateBox.setValue(person.getMedFormA());
			medicalExamDateBox.setValue(person.getMedFormB());
			holdHarmlessDateBox.setValue(person.getMedFormC());
			cellPhoneTextBox.setValue(person.getCellPhone());
			workPhoneTextBox.setValue(person.getWorkPhone());
			homePhoneTextBox.setValue(person.getHomePhone());
			emailAddressTextBox.setValue(person.getEmail());
			dateOfBirthDateBox.setValue(person.getDateOfBirth());
			activeCheckBox.setValue(person.getActive());		
			bsaIdTextBox.setValue(person.getBsaId());
			
		}
	
	}




}
